package yue.shen.model

import org.itheima.kotlin.game.core.Painter
import yue.shen.business.BlockAble
import yue.shen.business.MoveAble
import yue.shen.enums.Direction
import yue.shen.other.Config
import yue.shen.view.View

class Tank(override var x: Int, override var y: Int) : MoveAble {

    override var width: Int = Config.gameWidth
    override var height: Int = Config.gameHeight

    override var currentDirection: Direction = Direction.up
    override val speed: Int = 6

    //坦克不可以走的方向
    private var badDirection: Direction? = null

    override fun draw() {
        val imgPath = when (currentDirection) {
            Direction.up -> "/img/p1tankU.gif"
            Direction.down -> "/img/p1tankD.gif"
            Direction.right -> "/img/p1tankR.gif"
            Direction.left -> "/img/p1tankL.gif"
        }
        Painter.drawImage(imgPath, x, y)

    }

    fun move(direction: Direction) {

        //判断是否要往碰撞的方向
        if (direction == badDirection) return

        if (this.currentDirection != direction) {
            this.currentDirection = direction
            return
        }
        when (currentDirection) {
            Direction.up -> y -= speed
            Direction.down -> y += speed
            Direction.right -> x += speed
            Direction.left -> x -= speed
        }
        if (x < 0) x = 0
        if (y < 0) y = 0
        if (x > width - Config.block) x = width - Config.block
        if (y > height - Config.block) y = height - Config.block
    }

    override fun willCollision(blockAble: BlockAble): Direction? {
        //将要碰撞的判断
        var x = this.x
        var y = this.y
        when (currentDirection) {
            Direction.up -> y -= speed
            Direction.down -> y += speed
            Direction.right -> x += speed
            Direction.left -> x -= speed
        }

        //阻挡检测   检测下一步是否碰撞
        //1 阻挡物在上方
        var collision :Boolean= when {
            blockAble.y + Config.block <= y -> false
            blockAble.y - Config.block >= y -> false
            blockAble.x + Config.block <= x -> false
            blockAble.x - Config.block >= x -> false
            else -> true
        }
        return if(collision) currentDirection else null
    }

    override fun notifyBlock(direction: Direction?, blockAble: BlockAble?) {
        //接受碰撞信息
        this.badDirection = direction
    }

    fun shot() : Bullet{
        var bulletX = 0
        var bulletY = 0
        var tankX = x
        var tankY = y



        //计算子弹的坐标
        return Bullet(currentDirection,{
            bulletWidth,bulletHeight->
            when(currentDirection){
                Direction.up->{
                    bulletX = tankX+(Config.block - bulletWidth)/2
                    bulletY = tankY - bulletHeight/2
                }
                Direction.down->{
                    bulletX = tankX+(Config.block - bulletWidth)/2
                    bulletY = tankY + (Config.block - bulletHeight/2)
                }
                Direction.left->{
                    bulletX = tankX - bulletWidth/2
                    bulletY = tankY + (Config.block - bulletHeight)/2
                }
                Direction.right->{
                    bulletX = tankX + Config.block - bulletWidth/2
                    bulletY = tankY + (Config.block - bulletHeight)/2
                }
            }
            Pair(bulletX,bulletY)
        })
    }

}